﻿using System;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core.Data.Outlook;

namespace AnjLab.SyncIT.Core.Data._1C
{
    public class Event : ISourceTask
    {
        public string ID { get; set; }
        public string Summary { get; set; }
        public string Owner { get; set; }
        public string OwnerShortName { get; set; }
        public string Priority { get; set; }
        public string Status { get; set; }
        public string Description { get; set; }
        public string Content { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string Contractor { get; set; }

        public Importance Importance
        {
            get
            {
                switch (Priority)
                {
                    case "Высокая":
                        return Api.Importance.High;

                    case "Низкая":
                        return Api.Importance.Low;
                }

                //  Средняя
                return Api.Importance.Normal;
            }
        }

        public bool IsClosed
        {
            get
            {
                return string.Equals("Завершено", Status, StringComparison.InvariantCultureIgnoreCase)
                    || string.Equals("Отменено", Status, StringComparison.InvariantCultureIgnoreCase);
            }
        }

        public bool IsAssignedToMe(Source source)
        {
            return string.Equals(OwnerShortName, source.UserName, StringComparison.InvariantCultureIgnoreCase);
        }

        private void UpdateTaskDates(IOutlookItem outlookTask)
        {
            if (outlookTask.SupportsTimeRanging)
            {
                outlookTask.StartDate = DateTime.Parse(StartDate);
                outlookTask.DueDate = DateTime.Parse(EndDate);
            }
        }

        public void InitializeOutlookTask(IOutlookItem outlookTask)
        {
            UpdateTaskDates(outlookTask);
        }

        public void UpdateOutlookTask(IOutlookItem outlookTask)
        {
            UpdateTaskDates(outlookTask);
        }
    }
}
